package com.example.vp1;

import android.os.Bundle;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

public class MainActivity extends FragmentActivity {
	private ViewPager vp;
	private Fragment[] f4=new Fragment[4];
	private int[] color=new int[]{
		Color.BLUE,Color.RED,Color.GREEN,Color.YELLOW	
	};
	private ImageView[] ivs=new ImageView[4];
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		vp=(ViewPager) findViewById(R.id.vp);
		initfrag();
		ColorAdapter ca=new ColorAdapter(getSupportFragmentManager());
		vp.setAdapter(ca);
		ivs[0]=(ImageView) findViewById(R.id.i1);
		ivs[1]=(ImageView) findViewById(R.id.i2);
		ivs[2]=(ImageView) findViewById(R.id.i3);
		ivs[3]=(ImageView) findViewById(R.id.i4);
		vp.setOnPageChangeListener(new OnPageChangeAdapter());
		addListener();
	}
	private void initfrag(){
		for(int i=0;i<f4.length;i++){
			f4[i]=new VpFragment();
			Bundle b=new Bundle();
			b.putInt("color", color[i]);
			f4[i].setArguments(b);
		}
	}
	class ColorAdapter extends FragmentPagerAdapter{

		public ColorAdapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		@Override
		public Fragment getItem(int arg0) {
			// TODO Auto-generated method stub
			return f4[arg0];
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return f4.length;
		}
	}
	class OnPageChangeAdapter implements OnPageChangeListener{

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPageSelected(int arg0) {
			// TODO Auto-generated method stub
			for(int i=0;i<ivs.length;i++){
				ivs[i].setImageResource(R.drawable.w);
			}
			ivs[arg0].setImageResource(R.drawable.b);
		}
	}
	class OnChangePageListener implements OnClickListener{
		private int position;
		public OnChangePageListener (int position){
			super();
			this.position=position;
		}
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			vp.setCurrentItem(position);
			for(int i=0;i<ivs.length;i++){
				ivs[i].setImageResource(R.drawable.w);
			}
			ivs[position].setImageResource(R.drawable.b);
		}
	}
	private void addListener() {
        for (int i = 0; i < ivs.length; i++) {
             ivs[i].setOnClickListener(new OnChangePageListener(i));
        }
   }
}
